home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
mxcode
/
soundx
/
timerx.doc
< prev
next >
Wrap
Text File
|
1993-06-27
|
9KB
|
297 lines
Copyright 1993 by Peter Sprenger Pete@amber.dinoco.de
Muenchener Str.6
50170 Kerpen
Germany
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author Peter Sprenger
makes no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
What is the timerx? Timerx is a by-product that I have written in
two days to support my game library. It is a small library, that
supports the following things:
1. Joystick routines
2. Time measurement with timer 2
3. micro sec delays with and without timer 2
4. measurement of one vertical blank cycle of a ega,vga card
5. set own timer 0 intr and clock value without disturbing system
clock
6. set Real Time Clock periodically interrupt
7. lowlevel intr controller functions
This small library is FREE! I hope you understand that I minimze
the documentation, because I need a lot of time for my studies.
If somebody finds this routines extremely useful, every kinds of
donations are welcome.
No libraries included! It would made this archive bigger and more
important :-) to fill it with libraries, but since everybody uses
its own c compiler and memory model I think it's stupid!
You can compile it with every memory model you wish.
Please, if you have any suggestions, questions or you enhanced this
library contact me! My usenet address is:
Pete@amber.dinoco.de
Happy hacking,
Pete
-------------------- Documentation ----------------------------
1. Joystick routines
-----------------
extern WORD tax,tay,tbx,tby; /* values of potentiometer */
extern WORD ba1,ba2,bb1,bb2; /* button values; bit set = pressed */
void ReadTJoy(BYTE which);
void ReadTJoy2(BYTE which);
void ReadBJoy();
The procedures ReadTJoy and ReadTJoy2 do nearly the same. They fill
the variables tax,tay,tbx,tby with the joystick values. The variable
names correspond:
tax x-channel joystick a
tay y-channel joystick a
tbx x-channel joystick b
tby y-channel joystick b
The parameter which determines which channels to query. This prevents
from hitting the timeout, if one of the joysticks are missing.
The operation is: Set which to 0xf to scan all channels. When
ReadTJoy comes back the channels (joysticks) that are missing have an
zero in their variables.
76543210
which parameter: xxxxtttt if the bit is set the channel is scanned
aabb x = not used
xyxy
The procedure ReadTJoy uses Timer 2 to measure the joystick values.
It is machine independent. It will give you the same results if
you are on a 286 AT or a fast 486. Please initialize timer 2 before
use. This is done with InitT2().
ReadTJoy2 does'nt uses any timer. It IS machine dependant. I made it
just in case you need timer 2 for sound, etc... The routine is
balanced, so you get the same results independant from the joystick
values.
The Procedure ReadBJoy read the joystick button state. It fills the
variables ba1,ba2,bb1,bb2.
ba1 Button 1 joystick a
ba2 Button 2 joystick a
bb1 Button 1 joystick b
bb2 Button 2 joystick a
If the button is pressed, the variables are set to TRUE (nonzero).
2. Timer 2 routines
----------------
void InitT2();
You initialize Timer 2 with Modus 2 and preload value of 0 (2^16)
with the call InitT2().
I use timer 2, because Timer 0 could be programmed in another modus
and Timer 2 is not used except of some sound routines perhaps.
void timer_on();
With timer_on() you start time measurement. It reads the timer value
and stores it in an internal variable.
WORD timer_off();
Timer_off() stops the measurement. Again the timer value is loaded
and the difference is returned. If the time interval you wanted to
stop is greater 54ms the result is WRONG. This happens, because the
timer makes all 54ms one turnaround.
The result you get back is the time in clk's. One clk is 0.838 micro
sec, since the timer is clocked with 1.19318 Mhz. Sometimes it is
better to turn intr. off before time measurement. This is accurate,
but does not represent the real flow in your machine!
3. Micro sec delays
----------------
WORD to_micro(WORD clk);
The procedure to_micro converts from clk to micro sec. The
calculation is easy: micro_sec = clk * 838 / 1000
void clkdelay(WORD clicks);
Clkdelay waits a specified amount of clk's. Due to the overhead in
reading the timer, it's accuracy is about +-3 clk's.
This function is inaccurate with small delay values. Use clk values
greater 8.
void measure();
WORD mcalc(WORD micro);
void mdelay(WORD delay);
void msdelay(WORD delay);
The procedure measure() stops the time of a empty loop:
move cx,10000
label:
loop label
The result is stored in an internal variable.
Now if you want to get the loop repetition factor of e.g. 100 micro
sec, say: delayf = mcalc(100).
To wait now 100 micro sec, say: mdelay(delayf).
If you want to wait milisec (not microsec) use msdelay. It will call
delay * mdelay(mcalc(999)). Not mdelay(mcalc(1000)) because far call
and ret is about 1 microsec.
/* Example wait 100 micro sec */
WORD delayf;
InitT2(); /* only once */
measure(); /* only once */
delayf = mcalc(100);
mdelay(delayf);
/* Example end */
The procedure mdelay is only a loop, it uses NO timer!
4. Vertical Blank measurement
--------------------------
WORD vbl_measure();
The function vbl_measure returns the number of clk's between two
vertical retrace events.
5. Timer 0 interrupt
-----------------
void Install_Timer0(WORD period,void (*func)());
void Remove_Timer0();
With these procedures you can set your own timer 0 interrupt routine
and the time between the interrupts. The time parameter period is in
timer clk's, and the second parameter is the function pointer to
your function.
The use of Install_Timer0 does NOT disturb the system clock. The
msdos system clock routine is called with 18,206hz like before!
With Remove_Timer0 the old interrupt vector is restored, and timer
is set at the usual timer value of 0 (2^16).
6. Real Time Clock interrupt
-------------------------
void Install_RTC(WORD hertz,void (*func)());
void Remove_RTC();
When you call Install_RTC() you can activate the real time clock
periodical interrupt. With the argument hertz you can set the
interrupt rate between 2hz and 8192hz. The specs say that values
over 8192hz will not work. The 2nd argument is a function ptr to
the user function you want to call at the interrupt.
The procedure Remove_RTC() will disable the periodical interrupt at
the RTC and restore the old interrupt vector.
With Remove_RTC the old interrupt vector is restored, and the
interrupt disabled.
7. lowlevel intr controller procs
------------------------------
BYTE int2vect(BYTE intnr);
void enable_int(BYTE nr);
void disable_int(BYTE nr);
The procedure int2vect() gets the number of an interrupt and returns
the vector number corresponding to that interrupt. Very simple, only
design feature!
The procedures enable_int()/disable_int() gives you aid in
enabling/disabling single interrupts in the interrupt controller.
Call them with interrupt number you want to enable/disable.
-----------------------------------------------------------
Vers. History
-------
1.00 02-Jun-93 timerx 1.0 library released
1.10 10-Jun-93 msdelay() added
14-Jun-93 Real Time Clock functions added
15-Jun-93 missing cli bug in measure fixed
22-JUn-93 enable_int(),disable_int(),int2vect() added